home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / disasm.tar / disasm / instr.c < prev    next >
C/C++ Source or Header  |  1990-04-25  |  5KB  |  292 lines

  1. #include "dis48.h"
  2.  
  3. char    hex[] = "0123456789ABCDEF";
  4.  
  5. char    *InStr0[16] = {
  6.     "retsetxm", "ret", "retsetc", "retclrc", "sethex", "setdec",
  7.     "push.a\t\tc", "pop.a\t\tc", "clr.x\t\tst", "move.x\t\tst, c",
  8.     "move.x\t\tc, st", "swap.x\t\tc, st", "inc.1\t\tp", "dec.1\t\tp",
  9.     "AND/OR", "reti"
  10. };
  11.  
  12. char    *OpStr0[8] = {
  13.     "b, a", "c, b", "a, c", "c, d", "a, b", "b, c", "c, a", "d, c"
  14. };
  15.  
  16. char    *InStr9[8] = {"eq", "ne", "z", "nz", "gt", "lt", "ge", "le"};
  17.  
  18. char    *OpStr9[4] = {"a, b", "b, c", "c, a", "d, c"};
  19.  
  20. char    *OpStraf[16] = {
  21.     "b, a", "c, b", "a, c", "c, d", "a, a", "b, b", "c, c", "d, d",
  22.     "a, b", "b, c", "c, a", "d, c", "b, a", "c, b", "a, c", "c, d"
  23. };
  24.  
  25. #ifdef ANSI
  26. char    *Instr1(char *mem, NAddr *addr, char *out);
  27. char    *Instr8(char *mem, NAddr *addr, char *out);
  28. #else
  29. char    *Instr1();
  30. char    *Instr8();
  31. #endif
  32.  
  33. NAddr
  34. #ifdef ANSI
  35. Instruction(char *mem, NAddr addr, char *out)
  36. #else
  37. Instruction(mem, addr, out)
  38. char    *mem;
  39. NAddr    addr;
  40. char    *out;
  41. #endif
  42. {
  43.     Nybble    n;
  44.     Nybble    fn;
  45.     char    *p;
  46.     char    c;
  47.     int    disp, pc;
  48.     
  49.     switch (n = GetNybble(mem, addr++)) {
  50.     case 0:
  51.         if ((n = GetNybble(mem, addr++)) != 0xe) {
  52.             p = AppendStr(out, InStr0[n]);
  53.             if ((n < 4) || (n == 0xf))
  54.                 itype = ujump;
  55.                 
  56.             break;
  57.         }
  58.         
  59.         fn = GetNybble(mem, addr++);
  60.         n = GetNybble(mem, addr++);
  61.         p = AppendStr(out, (n < 8) ? "and" : "or");
  62.         p = AppendField(p, fn);
  63.         APPEND_TAB(p);
  64.         p = AppendStr(p, OpStr0[n & 7]);
  65.         break;
  66.         
  67.     case 1:
  68.         p = Instr1(mem, &addr, out);
  69.         break;
  70.         
  71.     case 2:
  72.         p = AppendStr(out, "move.1\t\t");
  73.         p = AppendImmNyb(p, mem, &addr, 1);
  74.         APPEND_COMMA(p);
  75.         APPEND_CHAR(p, 'p');
  76.         break;
  77.         
  78.     case 3:
  79.         p = AppendStr(out, "move.p");
  80.         fn = GetNybble(mem, addr++);
  81.         p = Append16(p, fn);
  82.         APPEND_TAB(p);
  83.         if ((p - out) < 8)
  84.             APPEND_TAB(p);
  85.             
  86.         p = AppendImmNyb(p, mem, &addr, fn + 1);
  87.         APPEND_COMMA(p);
  88.         APPEND_CHAR(p, 'c');
  89.         break;
  90.         
  91.     case 4:
  92.     case 5:
  93.         pc = addr;
  94.         disp = GetInt(mem, &addr, 2);
  95.         if (disp == 2) {
  96.             p = AppendStr(out, "nop3");
  97.             break;
  98.         }
  99.         
  100.         p = AppendStr(out, (disp == 0) ? "retc" : "brc");
  101.         APPEND_CHAR(p, (n == 4) ? 's' : 'c');
  102.         if (disp != 0) {
  103.             APPEND_TAB(p);
  104.             APPEND_TAB(p);
  105.             p = AppendRAddr(p, pc, disp, 2, 1);
  106.         } else
  107.             target = NOADDR;
  108.         
  109.         itype = branch;
  110.         break;
  111.     
  112.     case 6:
  113.         pc = addr;
  114.         disp = GetInt(mem, &addr, 3);
  115.         if ((disp == 3) || (disp == 4)) {
  116.             p = AppendStr(out, "nop");
  117.             APPEND_BDIGIT(p, disp + 1);
  118.             if (disp == 4)
  119.                 addr++;
  120.                 
  121.             break;
  122.         }
  123.             
  124.         p = AppendStr(out, "jump.3\t\t");
  125.         p = AppendRAddr(p, pc, disp, 3, 1);
  126.         itype = jump;
  127.         break;
  128.         
  129.     case 7:
  130.         pc = addr + 3;
  131.         disp = GetInt(mem, &addr, 3);
  132.         p = AppendStr(out, "call.3\t\t");
  133.         p = AppendRAddr(p, pc, disp, 3, 4);
  134.         itype = call;
  135.         break;
  136.         
  137.     case 8:
  138.         fn = GetNybble(mem, addr);    /* peek! */
  139.         if ((fn != 0xa) && (fn != 0xb)) {
  140.             p = Instr8(mem, &addr, out);
  141.             break;
  142.         }
  143.         
  144.         /* fall through */
  145.     case 9:
  146.         if (n == 8) {
  147.             c = (fn == 0xa) ? 0 : 1;
  148.             fn = 0xf;
  149.             addr++;
  150.             
  151.         } else {
  152.             fn = GetNybble(mem, addr++);
  153.             c = (fn < 8) ? 0 : 1;
  154.             fn &= 7;
  155.         }
  156.  
  157.         n = GetNybble(mem, addr++);
  158.         pc = addr;
  159.         disp = GetInt(mem, &addr, 2);
  160.         p = AppendStr(out, (disp == 0) ? "ret" : "br");
  161.         p = AppendStr(p, InStr9[((n >> 2) & 3) + 4 * c]);
  162.         p = AppendField(p, fn);
  163.         if ((p - out) < 8)
  164.             APPEND_TAB(p);
  165.             
  166.         if ((c == 0) && (n >= 8)) {
  167.             APPEND_CHAR(p, (n & 3) + 'a');
  168.             
  169.         } else {
  170.             p = AppendStr(p, OpStr9[n & 3]);
  171.         }
  172.         
  173.         if (disp != 0) {
  174.             APPEND_COMMA(p);
  175.             p = AppendRAddr(p, pc, disp, 2, 3);
  176.         } else
  177.             target = NOADDR;
  178.         
  179.         itype = branch;
  180.         break;
  181.         
  182.     default:
  183.         switch (n) {
  184.         case 0xa:
  185.             fn = GetNybble(mem, addr++);
  186.             c = (fn < 8) ? 0 : 1;
  187.             fn &= 7;
  188.             disp = 0xa;
  189.             break;
  190.             
  191.         case 0xb:
  192.             fn = GetNybble(mem, addr++);
  193.             c = (fn < 8) ? 0 : 1;
  194.             fn &= 7;
  195.             disp = 0xb;
  196.             break;
  197.             
  198.         case 0xc:
  199.         case 0xd:
  200.             fn = 0xf;
  201.             c = n & 1;
  202.             disp = 0xa;
  203.             break;
  204.             
  205.         case 0xe:
  206.         case 0xf:
  207.             fn = 0xf;
  208.             c = n & 1;
  209.             disp = 0xb;
  210.             break;
  211.         }
  212.         
  213.         n = GetNybble(mem, addr++);
  214.         pc = 0;
  215.         switch (disp) {
  216.         case 0xa:
  217.             if (c == 0) {
  218.                 if (n < 0xc) {
  219.                     p = "add";
  220.                 
  221.                 } else {
  222.                     p = "dec";
  223.                     pc = 1;
  224.                 }
  225.                 
  226.             } else {
  227.                 if (n < 4) {
  228.                     p = "clr";
  229.                     pc = 1;
  230.                     
  231.                 } else if (n >= 0xc) {
  232.                     p = "swap";
  233.                 
  234.                 } else {
  235.                     p = "move";
  236.                     if (n < 8)
  237.                         n -= 4;
  238.                 }
  239.             }
  240.             
  241.             break;
  242.             
  243.         case 0xb:
  244.             if (c == 0) {
  245.                 if (n >= 0xc) {
  246.                     p = "subn";
  247.                 
  248.                 } else if ((n >= 4) && (n <= 7)) {
  249.                     p = "inc";
  250.                     pc = 1;
  251.                     n -= 4;
  252.                 
  253.                 } else {
  254.                     p = "sub";
  255.                 }
  256.                 
  257.             } else {
  258.                 pc = 1;
  259.                 if (n < 4)
  260.                     p = "sln";
  261.                     
  262.                 else if (n < 8)
  263.                     p = "srn";
  264.                     
  265.                 else if (n < 0xc)
  266.                     p = "neg";
  267.                     
  268.                 else
  269.                     p = "not";
  270.             }
  271.             
  272.             break;
  273.         }
  274.         
  275.         p = AppendStr(out, p);
  276.         p = AppendField(p, fn);
  277.         if ((p - out) < 9)
  278.             APPEND_TAB(p);
  279.             
  280.         if (pc == 1)
  281.             APPEND_CHAR(p, (n & 3) + 'a');
  282.         
  283.         else
  284.             p = AppendStr(p, OpStraf[n]);
  285.             
  286.         break;
  287.     }
  288.     
  289.     TERMINATE(p);
  290.     return(addr);
  291. }
  292.